#     TextLocutor
#     Copyright (C) 2009  Jose Ignacio Naranjo Hernandez (joseignacio.naranjo@gmail.com)

#     This program is free software: you can redistribute it and/or modify
#     it under the terms of the GNU Affero General Public License as
#     published by the Free Software Foundation, either version 3 of the
#     License, or (at your option) any later version.

#     This program is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU Affero General Public License for more details.

#     You should have received a copy of the GNU Affero General Public License
#     along with this program.  If not, see <http://www.gnu.org/licenses/>.

try:
  from xml.etree import ElementTree # for Python 2.5 users
except ImportError:
  from elementtree import ElementTree
import gdata.calendar.service
import gdata.service
import atom.service
import gdata.calendar
import atom
import getopt
import sys
import string
import time
import logging
import gdata.alt.appengine

class Calendar:
  def __init__(self, token):
    self.gcal = gdata.calendar.service.CalendarService()
    self.gcal.SetAuthSubToken(token)
    gdata.alt.appengine.run_on_appengine(self.gcal)

  def CreateCalendar(self, title, description, time_zone, location, hidden=False, color=None): 
    calendar = gdata.calendar.CalendarListEntry()
    calendar.title = atom.Title(text=title)
    calendar.summary = atom.Summary(text=description)
    calendar.where = gdata.calendar.Where(value_string=location)
    #    calendar.color = gdata.calendar.Color(value=color)
    calendar.timezone = gdata.calendar.Timezone(value=time_zone)  
    
    if hidden:
      calendar.hidden = gdata.calendar.Hidden(value='true')
    else:
      calendar.hidden = gdata.calendar.Hidden(value='false')
        
    self.gcal.InsertCalendar(new_calendar=calendar)


  def _InsertEvent(self, title, content, where, start_time, end_time, id_cal):
    event = gdata.calendar.CalendarEventEntry()
    event.title = atom.Title(text=title)
    event.content = atom.Content(text=content)
    event.where.append(gdata.calendar.Where(value_string=where))
    
    if start_time is None:
      # Use current time for the start_time
      start_time = time.strftime('%Y-%m-%dT%H:%M:%S.000Z', time.gmtime(time.time() + 360))
      end_time = time.strftime('%Y-%m-%dT%H:%M:%S.000Z', time.gmtime(time.time() + 420))

    event.when.append(gdata.calendar.When(start_time=start_time, end_time=end_time))
    new_event = self.gcal.InsertEvent(event, id_cal)
    self._AddReminder(new_event, minutes=5)


  def _AddReminder(self, event, minutes=5):
    for a_when in event.when:
      if len(a_when.reminder) > 0:
        a_when.reminder[0].minutes = minutes
      else:
        a_when.reminder.append(gdata.calendar.Reminder(minutes=minutes, method='sms'))

    return self.gcal.UpdateEvent(event.GetEditLink().href, event)


  def _GetID(self, title):
    feed = self.gcal.GetAllCalendarsFeed()

    for e in feed.entry:
      if e.title.text == title:
        # the model stores a string as ID
        logging.debug("The calendar exists")
        return str(e.GetAlternateLink().href)

    return None


#  def Run(self):
#    self._CreateCalendar(title='prueba', description='prueba', time_zone='Europe/Spain', location='Madrid')
#    self._GetUserCalendars()

